package com.white.androidstudy.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by Administrator on 2017/11/6 0006.
 */

public class ScrollGroup extends ViewGroup
{
    public ScrollGroup(Context context)
    {
        super(context);
        init(context);
    }

    public ScrollGroup(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    Scroller mScroller;
    private int mTouchSlop;
    private int mTopBorder, mBottomBorder, finalScrollY;

    private void init(Context context)
    {
        mScroller = new Scroller(context);
        ViewConfiguration config = ViewConfiguration.get(context);
        mTouchSlop = config.getScaledPagingTouchSlop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < getChildCount(); i++)
        {
            View c = getChildAt(i);
            measureChild(c, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        if (changed)
        {
            int c = getChildCount();
            MarginLayoutParams p = (MarginLayoutParams) getLayoutParams();
            int vHeight = getMeasuredHeight();
            p.height = vHeight * c;
            setLayoutParams(p);
            for (int i = 0; i < c; i++)
            {
                View v = getChildAt(i);
                if (v.getVisibility() != GONE)
                {
                    v.layout(l, i * vHeight, r, (i + 1) * vHeight);
                }
            }
            if (c > 0)
            {
                mTopBorder = getChildAt(0).getTop();
                mBottomBorder = getChildAt(c - 1).getBottom();
                finalScrollY = getChildAt(c - 1).getTop();
            }
        }
    }

    float mYDown, mYMove, mLastMoveY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                mYDown = ev.getY();
                mLastMoveY = mYDown;
                break;
//            case MotionEvent.ACTION_MOVE:
//                Log.e("s", "进入 onInterceptTouchEvent的 ACTION_MOVE");
//                mYMove = ev.getY();
//                float diff = Math.abs(mYMove - mYDown);
//                mLastMoveY = mYDown;
//                if (diff > mTouchSlop)
//                    return true;
//                break;
//            case MotionEvent.ACTION_UP:
//                Log.e("s", "进入 onInterceptTouchEvent的 ACTION_MOVE");
//                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    float mLastY, mLastX;

    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        switch (e.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                return true;
            case MotionEvent.ACTION_MOVE:
                mYDown = e.getY();
                int scrollY = (int) (mLastMoveY - mYDown);
                if (getScrollY() + scrollY < mTopBorder)
                {
                    scrollTo(0, mTopBorder);
                    return true;
                } else if (getScrollY() + getHeight() + scrollY > mBottomBorder)
                {
                    scrollTo(0, finalScrollY);
                    return true;
                }
                Log.e("s", "scrollY = " + getScrollY() + "--height = " + getHeight() +
                        "---scrollY = " + scrollY + "---mBOttomBorder = " + mBottomBorder);
                scrollBy(0, scrollY);
                mLastMoveY = mYDown;
                break;
            case MotionEvent.ACTION_UP:
                int targetIndex = (getScrollY() + getHeight() / 2) / getHeight();
                int dy = targetIndex * getHeight() - getScrollY();
                mScroller.startScroll(0, getScrollY(), 0, dy);
                invalidate();
                break;
        }
        return super.onTouchEvent(e);
    }

    @Override
    public void computeScroll()
    {
        if (mScroller.computeScrollOffset())
        {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }
}
